package com.grape.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grape.constants.Constants;
import com.grape.error.BusinessException;
import com.grape.utils.DateUtils;
import com.grape.utils.SpringUtil;
import com.grape.utils.StringUtils;
import com.grape.utils.ThreadLocalUtil;
import com.grape.vo.BaseVo;
import com.grape.vo.MemberOpenVO;
import com.grape.wrapper.BaseWrapper;
import com.grape.domain.Member;
import com.grape.domain.MemberGradePrice;
import com.grape.mapper.MemberGradePriceMapper;
import com.grape.mapper.MemberMapper;
import com.grape.service.IMemberService;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 *
 * 会员表 服务实现类
 *
 * @author      putao
 * @email       2628279194@qq.com
 */
@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    private final MemberMapper memberMapper;

    private final MemberGradePriceMapper memberGradePriceMapper;

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(MemberServiceImpl.class);

    /**
     * 分页
     * @param baseVo    统一分页对象
     * @return          会员表集合
     */
    @Override
    public List<Member> listData(BaseVo baseVo) {
        QueryWrapper<Member> wrapper = new BaseWrapper<Member>().init();
        wrapper.like(Strings.isNotBlank(baseVo.getSearch()), Constants.NAME, baseVo.getSearch())
                .eq(StringUtils.isNotBlank(baseVo.getColumnName()) && StringUtils.isNotBlank(baseVo.getSelect()), baseVo.getColumnName(), baseVo.getSelect());
        orderByModifyTimeDesc(wrapper);
        return this.list(wrapper);
    }

    /**
     * 自定义分页
     * @param   baseVo      统一分页对象
     */
    @Override
    public void pageListCustom(BaseVo baseVo) {
        QueryWrapper<Member> qw = new BaseWrapper<Member>().init(ThreadLocalUtil.getUserId())
                .orderByDesc(Constants.CREATE_TIME);
        this.memberMapper.selectList(qw);
    }

    /**
     * 开通会员
     * @param memberOpenVO 会员表对象
     * @return 会员表对象
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Member insertData(MemberOpenVO memberOpenVO) {
        MemberGradePrice memberGradePrice = memberGradePriceMapper.selectOne(new BaseWrapper<MemberGradePrice>().init().eq(Constants.ID, memberOpenVO.getMemberGradePriceId()));
        if (memberGradePrice == null) {
            throw new BusinessException("找不到会员价格信息，无法开通");
        }
        Member member = new Member();
        member.setStatus(Constants.ONE_STR);
        member.setLevel(memberGradePrice.getLevel());
        member.setName(memberGradePrice.getName());
        member.setStartTime(new Date());
        Date date = new Date();
        if (Constants.MEMBER_TIME_TYPE_PERMANENT.equals(memberGradePrice.getTimeType())) {
            member.setEndTime(null);
        } else if (Constants.MEMBER_TIME_TYPE_YEAR.equals(memberGradePrice.getTimeType())) {
            member.setEndTime(DateUtils.addDays(member.getStartTime(), (DateUtils.getDaysByYear(date) + 1), true));
        } else if (Constants.MEMBER_TIME_TYPE_QUARTER.equals(memberGradePrice.getTimeType())) {
            member.setEndTime(DateUtils.addDays(member.getStartTime(), (DateUtils.getDaysByQuarter(date) + 1), true));
        } else if (Constants.MEMBER_TIME_TYPE_MONTH.equals(memberGradePrice.getTimeType())) {
            member.setEndTime(DateUtils.addDays(member.getStartTime(), (DateUtils.getDaysByMonth(date) + 1), true));
        }
        Member insert = this.baseInsert(member);
        // TODO
//        ISysUserService userService = SpringUtil.getBean(ISysUserService.class);
//        userService.updateUserMember();
        return insert;
    }

    /**
     * 查询全部
     * @return      会员表集合
     */
    @Override
    public List<Member> listAll() {
        List<Member> memberList = memberMapper.selectList(new BaseWrapper<Member>().init().eq(Constants.STATUS, Constants.ONE_STR));
        if (CollectionUtil.isEmpty(memberList)) {
            return Collections.emptyList();
        }
        return memberList;
    }
}
